
const { ipcRenderer } = require('electron');
const { BrowserWindow, getGlobal, getCurrentWindow } = require("@electron/remote/renderer")
const { Menu } = require('@electron/remote')
import { winURL } from './config';
const path = require("path");

const menuContextTemplate = [
  {
    label: '复制',
    role: 'copy'
  },
  {
    label: "其他事件",
    click: () => {
      console.log('其他事件')
    }
  }
]


window.electronAPI = {
  openFile: (data) => ipcRenderer.invoke('openFile', data),
  notification: (msg) => ipcRenderer.invoke('notification', msg),
  setWinSize: (data) => ipcRenderer.invoke('setWinSize', data),
  closeWindowl: (url) => ipcRenderer.invoke(`close${url}`),
  setMinimumSize: (options) => ipcRenderer.invoke('setMinimumSize', options),
  setMinimize: () => ipcRenderer.invoke('setMinimize'),
  setMaximize: (screen) => ipcRenderer.invoke('setMaximize', screen),
  hide: () => ipcRenderer.invoke('hide'),
  showMessage: (options) => ipcRenderer.invoke('showMessage', options),
  setResizable: (data) => ipcRenderer.invoke('setResizable', data),
  openNativeWindow: (url, options = { minHeight: 630 }) => {
    if (window.nativeWindow && window.nativeWindow[url]) {
      return window.nativeWindow[url]
    }

    const secondWin = new BrowserWindow({
      frame: false,
      hasShadow: true,
      transparent: true,
      fullscreen: false,
      show: false,
      center: true,
      hasShadow: true,
      icon: path.join(__static, 'logo.png'),
      ...options,
      webPreferences: {
        preload: path.join(__dirname, 'imgload.js'),
        enableRemoteModule: true,
        nodeIntegration: true,
        contextIsolation: false,
        webSecurity: false,
        nodeIntegrationInSubFrames: true,
        allowRunningInsecureContent: true,
      }
    });
    if (process.env.WEBPACK_DEV_SERVER_URL) {
      secondWin.loadURL(process.env.WEBPACK_DEV_SERVER_URL + '#' + url);
      secondWin.webContents.openDevTools()
    } else {
      secondWin.loadURL(winURL + '#' + url)
    }
    if (window.nativeWindow) {
      if (!window.nativeWindow.url) {
        window.nativeWindow[url] = secondWin
      }
    } else {
      window.nativeWindow = {}
      window.nativeWindow[url] = secondWin
    }

    secondWin.on('closed', () => {
      window.nativeWindow[url] = null
    });

    return new Promise((resolve) => {
      secondWin.on('ready-to-show', () => {
        secondWin.show()
        resolve(secondWin)
      })
    })
  },
  sendMsg: (data) => ipcRenderer.invoke('sendMsg', data),

  twinkle: (options) => ipcRenderer.invoke('twinkle', options),
  openAutoStart: () => ipcRenderer.send('openAutoStart'),
  closeAutoStart: () => ipcRenderer.send('closeAutoStart'),
  getLoginItemSettings: () => ipcRenderer.invoke('getLoginItemSettings'),
  switchPage: () => ipcRenderer.send('switchPage'),
  //右键打开自定义菜单
  openRightClickMenu: (option) => {
    const menuBuilder = Menu.buildFromTemplate(option)
    menuBuilder.popup({
      window: getCurrentWindow
    })
  }
}

